package com.dementors.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.dementors.R;
import com.dementors.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 ************************
 *author:raochen
 *time:${DATE}.
 *name:今日特卖的fragment
 *step:
 */

public class FragmentToday extends BaseFragment {
  private TabLayout mTabLayout;
  private ViewPager vPager;
  private MyPagerAdapter mAdapter;
  private FragmentManager fm;
  private List<String> mTitleList = new ArrayList<>();//页卡标题集合
  private List<Fragment> mViewList = new ArrayList<>();//页卡视图集合

  @Override
  protected int provideContentViewId() {
    return R.layout.fragment_today;
  }

  @Override
  protected void initViews() {
    mTabLayout = (TabLayout) view.findViewById(R.id.tabs_today);
    vPager = (ViewPager) view.findViewById(R.id.vp_view_today);
  }

  @Override
  protected void initData() {
    initFragment();//初始化Fragment1中的每个小Fragment
    initTitle();//初始化标题
    initTablayout();//初始化tablayout
  }

  private void initTitle() {
    mTitleList.add("上新");
    mTitleList.add("童装");
    mTitleList.add("童鞋");
    mTitleList.add("用品");
    mTitleList.add("玩具");
    mTitleList.add("女装");
    mTitleList.add("居家");
    mTitleList.add("食品");
    mTitleList.add("美妆");

  }

  private void initTablayout() {
    mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    for(int i=0;i<9;i++) {
      mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(i)));//添加tab选项卡
    }
    fm=getChildFragmentManager();

    mAdapter = new MyPagerAdapter(fm);
    vPager.setAdapter(mAdapter);//给ViewPager设置适配器
    mTabLayout.setupWithViewPager(vPager);//将TabLayout和ViewPager关联起来。
    mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
  }


  private void initFragment() {
    NewFragment news=new NewFragment();
    mViewList.add(news);
    for (int i = 0; i < 8; i++) {
      DressFragment frag = new DressFragment();
      Bundle bundle=new Bundle();
      bundle.putInt("type",i);
      frag.setArguments(bundle);
      mViewList.add(frag);
    }

  }


  //ViewPager适配器
  class MyPagerAdapter extends FragmentPagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Override
    public int getCount() {
      return mViewList==null?0:mViewList.size();//页卡数
    }

    @Override
    public Fragment getItem(int position) {
      return mViewList.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return mTitleList.get(position);
    }
  }


}
